﻿<?php
//require ("fpdf.php");
require ("WriteHTML.php");
require("db/connect.php");
require("funcsubstr.php");

class PDF extends PDF_HTML {
	
	function Header()
	{
	$this->Image('img/pyk.JPG',15, 5, 50,0);
	$this->SetFont ( 'EDPenSook', '', 18);
	//iconv("UTF-8","TIS-620",$resultcust["cus_addr"])"ห.จ.ก.เครื่องเคลือบดินเผา PYK"
	$this->Cell(0,10,"ห.จ.ก.เครื่องเคลือบดินเผา PYK",0,1,"C");
	$this->SetFont ( 'EDPenSook', '', 12);
	$this->Cell(0,5,"เลขที่ 306/ข ม.6 ต.บางปลากด",0,1,"C");
	$this->Cell(0,5,"อ.ป่าโมก จ.อ่างทอง 14130",0,1,"C");
	$this->Ln(20);
	}
	
	function genHeaderCustomer(){
		$sqlcust="select * from orders,customer,tambol,amphur,province where orders.cus_id=customer.cus_id and customer.tambol_id=tambol.tambol_id and customer.amphur_id=amphur.amphur_id and customer.province_id=province.province_id and orders.orders_id='".$_GET["orders_id"]."'";
		$querycust=mysql_query($sqlcust);
		$resultcust=mysql_fetch_array($querycust);
		$this->SetFont( 'EDPenSook', '',  14);
		$this-> SetY(20);
		$this->Cell(0,10,"เลขที่ใบสั่งซื้อ/No: ".$resultcust["orders_id"],0,1,"R");
		$this->Ln(20);
		$cusName=iconv("UTF-8","TIS-620",$resultcust["cus_name"]."  ".$resultcust["cus_lastname"]);
		$this->Write(5,"ชื่อ : ".$cusName);
		$this->Ln(5);
		$cusAddr=iconv("UTF-8","TIS-620",$resultcust["cus_addr"]);
		$cusTambol=iconv("UTF-8","TIS-620",$resultcust["tambol_name"]);
		$cusAmphur=iconv("UTF-8","TIS-620",$resultcust["amphur_name"]);
		$cusProvince=iconv("UTF-8","TIS-620",$resultcust["province_name"]);
		$this->Write(5,"ที่อยู่ : ".$cusAddr."\nตำบล : ".$cusTambol."\nอำเภอ : ".$cusAmphur."\nจังหวัด : ".$cusProvince);
		$this->Ln(-20);
		$this-> SetY(45);
		$this->Cell(90,35,"",1,1,"C");
		$this-> SetY(50);
		$this-> SetX(110);
		$year=substr($resultcust["orders_date"],0,4)+543;
		$month=$this->monthconv(substr($resultcust["orders_date"],5,2));
		$day=substr($resultcust["orders_date"],8,2);
		$this->Write(3,"วันที่สั่งซื้อ : ".$day." ".$month." ".$year);
		$this->Ln(5);
		$this-> SetY(65);
		$this-> SetX(110);
		$this->Write(3,"สถานะการสั่งซื้อ : ".$this->orderstatus($resultcust["orders_status"]));
		$this->Ln(5);
		$this-> SetY(45);
		$this-> SetX(110);
		$this->Cell(90,35,"",1,1,"C");
	}
	
	function genHeaderEmployee(){
		$sqlcust="select * from ordersmaterial,employee,tambol,amphur,province where ordersmaterial.emp_id=employee.emp_id and employee.tambol_id=tambol.tambol_id and employee.amphur_id=amphur.amphur_id and employee.province_id=province.province_id and ordersmaterial.ordermat_id='".$_GET["orders_id"]."'";
		
		$querycust=mysql_query($sqlcust);
		$resultcust=mysql_fetch_array($querycust);
		$this->SetFont( 'EDPenSook', '',  14);
		$this-> SetY(20);
		$this->Cell(0,10,"เลขที่ใบสั่งซื้อ/No: ".$resultcust["ordermat_id"],0,1,"R");
		$this->Ln(20);
		$cusName=iconv("UTF-8","TIS-620",$resultcust["emp_name"]."  ".$resultcust["emp_lastname"]);
		$this->Write(5,"ชื่อ : ".$cusName);
		$this->Ln(5);
		$cusAddr=iconv("UTF-8","TIS-620",$resultcust["emp_addr"]);
		$cusTambol=iconv("UTF-8","TIS-620",$resultcust["tambol_name"]);
		$cusAmphur=iconv("UTF-8","TIS-620",$resultcust["amphur_name"]);
		$cusProvince=iconv("UTF-8","TIS-620",$resultcust["province_name"]);
		$this->Write(5,"ที่อยู่ : ".$cusAddr."\nตำบล : ".$cusTambol."\nอำเภอ : ".$cusAmphur."\nจังหวัด : ".$cusProvince);
		$this->Ln(-20);
		$this-> SetY(45);
		$this->Cell(90,35,"",1,1,"C");
		$this-> SetY(50);
		$this-> SetX(110);
		$year=substr($resultcust["ordersmat_date"],0,4)+543;
		$month=$this->monthconv(substr($resultcust["ordersmat_date"],5,2));
		$day=substr($resultcust["ordersmat_date"],8,2);
		$this->Write(3,"วันที่สั่งซื้อ : ".$day." ".$month." ".$year);
		$this->Ln(5);
		$this-> SetY(65);
		$this-> SetX(110);
		$this->Write(3,"สถานะการสั่งซื้อ : ".$this->orderstatus($resultcust["ordersmat_status"]));
		$this->Ln(5);
		$this-> SetY(45);
		$this-> SetX(110);
		$this->Cell(90,35,"",1,1,"C");
	}
	
		function monthconv($month){
		switch($month){
			case "01":return "มกราคม";break;
			case "02":return "กุมภาพันธ์";break;
			case "03":return "มีนาคม";break;
			case "04":return "เมษายน";break;
			case "05":return "พฤษภาคม";break;
			case "06":return "มิถุนายน";break;
			case "07":return "กรกฏาคม";break;
			case "08":return "สิงหาคม";break;
			case "09":return "กันยายน";break;
			case "10":return "ตุลาคม";break;
			case "11":return "พฤศจิกายน";break;
			case "12":return "ธันวาคม";break;
			}
		}
	
	function genFooter(){
				$sqlcust="select * from orders,customer,tambol,amphur,province,employee  where orders.cus_id=customer.cus_id and customer.tambol_id=tambol.tambol_id and customer.amphur_id=amphur.amphur_id and customer.province_id=province.province_id and employee.emp_id=orders.emp_id and orders.orders_id='".$_GET["orders_id"]."'";
		$querycust=mysql_query($sqlcust);
		$resultcust=mysql_fetch_array($querycust);
		$cusName=iconv("UTF-8","TIS-620",$resultcust["cus_name"]."  ".$resultcust["cus_lastname"]);
		$empName=iconv("UTF-8","TIS-620",$resultcust["emp_name"]."  ".$resultcust["emp_lastname"]);
		
		$this->SetFillColor ( 255, 255,255);
		$this->SetDrawColor ( 0);
		$this-> SetX(10);
		$this->MultiCell(90,50,"",1,1,"L",false);
		$this->Ln (-50);
		$this-> SetX(110);
		$this->MultiCell(90,50,"",1,1,"R",false);
		$this->Ln (-55);
		$this->Cell(90,40,".......................................................................................",0,1,"C",false);
		$this->Ln (-33);
		$this->Cell(90,40,"(คุณ ".$cusName.")",0,1,"C",false);
		$this->Ln (-32);
		$this->Cell(90,40,"ผู้ชำระเงิน",0,1,"C",false);
		$this->Ln (-30);
		$this->Cell(90,40,"วันที่............/............/................",0,1,"C",false);
		//////// Footer Right Start
		$this->Ln (-70);
		$this-> SetX(110);
		$this->Cell(90,50,".......................................................................................",0,1,"C",false);
		$this->Ln (-38);
		$this-> SetX(110);
		$this->Cell(90,40,"(คุณ ".$empName.")",0,1,"C",false);
		$this->Ln (-32);
		$this-> SetX(110);
		$this->Cell(90,40,"พนักงานผู้รับผิดชอบ",0,1,"C",false);
		$this->Ln (-30);
		$this-> SetX(110);
		$this->Cell(90,40,"วันที่............/............/................",0,1,"C",false);
	}
	
	function genFooter2(){
				$sqlcust="select * from ordersmaterial,tambol,amphur,province,employee  where ordersmaterial.emp_id=employee.emp_id and employee.tambol_id=tambol.tambol_id and employee.amphur_id=amphur.amphur_id and employee.province_id=province.province_id  and ordersmaterial.ordermat_id='".$_GET["orders_id"]."'";
		$querycust=mysql_query($sqlcust);
		//echo $sqlcust;
		$resultcust=mysql_fetch_array($querycust);
		$cusName=iconv("UTF-8","TIS-620",$resultcust["emp_name"]."  ".$resultcust["emp_lastname"]);
		$empName=iconv("UTF-8","TIS-620"," รวย มากมาย");
		
		$this->SetFillColor ( 255, 255,255);
		$this->SetDrawColor ( 0);
		$this-> SetX(10);
		$this->MultiCell(90,50,"",1,1,"L",false);
		$this->Ln (-50);
		$this-> SetX(110);
		$this->MultiCell(90,50,"",1,1,"R",false);
		$this->Ln (-55);
		$this->Cell(90,40,".......................................................................................",0,1,"C",false);
		$this->Ln (-33);
		$this->Cell(90,40,"(คุณ ".$cusName.")",0,1,"C",false);
		$this->Ln (-32);
		$this->Cell(90,40,"พนักงานผู้รับผิดชอบ",0,1,"C",false);
		$this->Ln (-30);
		$this->Cell(90,40,"วันที่............/............/................",0,1,"C",false);
		//////// Footer Right Start
		$this->Ln (-70);
		$this-> SetX(110);
		$this->Cell(90,50,".......................................................................................",0,1,"C",false);
		$this->Ln (-38);
		$this-> SetX(110);
		$this->Cell(90,40,"(คุณ )",0,1,"C",false);
		$this->Ln (-32);
		$this-> SetX(110);
		$this->Cell(90,40,"ผู้จัดการ",0,1,"C",false);
		$this->Ln (-30);
		$this-> SetX(110);
		$this->Cell(90,40,"วันที่............/............/................",0,1,"C",false);
	}
	
	function genHeaderTable($header) {
		// Colors, line width and bold font
		
	
		$this->SetFillColor ( 0, 50,100);
		$this->SetTextColor ( 255 );
		$this->SetDrawColor ( 0, 68, 0 );
		$this->SetLineWidth ( .3 );
		$this->SetFont ( 'EDPenSook', '', 14);
		//$this->SetFont ( '', 'B' );
		// Header
		$w = array (
				40,
				 65,
				40,
				45 
		);
		for($i = 0; $i < count ( $header ); $i ++)
			$this->Cell ( $w [$i], 7, $header [$i], 1, 0, 'C', true );
		$this->Ln ();
		// Color and font restoration
		$this->SetFillColor ( 224, 235, 255 );
		$this->SetTextColor ( 0 );
		$this->SetFont ( '' );
		// Data
		$fill = false;
		$i=1;
			$sqlcart="select * from orders,cart,product where orders.orders_id=cart.orderid and cart.pro_id=product.pro_id and  orders.orders_id='".$_GET["orders_id"]."'";
		
		$querycart=mysql_query($sqlcart);
		$sum=0;
			while($resultcart=mysql_fetch_array($querycart)){
					$this->Cell ( $w [0], 6,$i, 'LR', 0, 'C', $fill );
					$proName=iconv("UTF-8","TIS-620",$resultcart["pro_name"].color($resultcart["pro_color"]));
			$this->Cell ( $w [1], 6,$proName, 'LR', 0, 'L', $fill );
			$this->Cell ( $w [2], 6, number_format ( $resultcart["pro_price"]), 'LR', 0, 'R', $fill );
			$this->Cell ( $w [3], 6, number_format ( $resultcart["qty"]), 'LR', 0, 'R', $fill );
			$this->Ln ();
			$fill = ! $fill;
				$sum=$sum+( $resultcart["qty"]*$resultcart["pro_price"]);
			$i++;
				}
		//for($i=1;$i<=5;$i++) {
	//}
		// Closing line
		$this->SetFillColor ( 255, 255,0);
		$this->Cell ( 145, 6,"รวม", 'LTRB', 0, 'L',true);
		$this->Cell ( 45, 6, number_format ($sum),'LTRB',0, 'R',true);
		$this->Ln ();
			$this->SetFillColor (0, 255,0);
		$this->Cell ( 145, 6,"VAT 7%", 'LTRB', 0, 'L',true);
		$vat=(7*$sum)/100;
		$this->Cell ( 45, 6, number_format ($vat),'LTRB',0, 'R',true);
		$this->Ln ();
			$this->SetFillColor (255,255,255);
		$this->Cell ( 145, 6,"รวมทั้งหมด", 'LTRB', 0, 'L',true);
		$sumnet=$sum+$vat;
		$this->Cell ( 45, 6, number_format ($sumnet),'LTRB',0, 'R',true);
		$this->Ln ();
	}
	
	function genHeaderTable2($header) {
		// Colors, line width and bold font
		
	
		$this->SetFillColor ( 0, 50,100);
		$this->SetTextColor ( 255 );
		$this->SetDrawColor ( 0, 68, 0 );
		$this->SetLineWidth ( .3 );
		$this->SetFont ( 'EDPenSook', '', 14);
		//$this->SetFont ( '', 'B' );
		// Header
		$w = array (
				40,
				 65,
				40,
				45 
		);
		for($i = 0; $i < count ( $header ); $i ++)
			$this->Cell ( $w [$i], 7, $header [$i], 1, 0, 'C', true );
		$this->Ln ();
		// Color and font restoration
		$this->SetFillColor ( 224, 235, 255 );
		$this->SetTextColor ( 0 );
		$this->SetFont ( '' );
		// Data
		$fill = false;
		$i=1;
			$sqlcart="select * from ordersmaterial,cartmaterial,material where ordersmaterial.ordermat_id=cartmaterial.ordermat_id and cartmaterial.mat_id=material.mat_id and  ordersmaterial.ordermat_id='".$_GET["orders_id"]."'";
		
		$querycart=mysql_query($sqlcart);
		$sum=0;
			while($resultcart=mysql_fetch_array($querycart)){
					$this->Cell ( $w [0], 6,$i, 'LR', 0, 'C', $fill );
					$proName=iconv("UTF-8","TIS-620",$resultcart["mat_name"]);
			$this->Cell ( $w [1], 6,$proName, 'LR', 0, 'L', $fill );
			$this->Cell ( $w [2], 6, number_format ( $resultcart["mat_price"]), 'LR', 0, 'R', $fill );
			$this->Cell ( $w [3], 6, number_format ( $resultcart["mat_qty"]), 'LR', 0, 'R', $fill );
			$this->Ln ();
			$fill = ! $fill;
				$sum=$sum+( $resultcart["mat_qty"]*$resultcart["mat_price"]);
			$i++;
				}
		//for($i=1;$i<=5;$i++) {
	//}
		// Closing line
		$this->SetFillColor ( 255, 255,0);
		$this->Cell ( 145, 6,"รวม", 'LTRB', 0, 'L',true);
		$this->Cell ( 45, 6, number_format ($sum),'LTRB',0, 'R',true);
		$this->Ln ();
			$this->SetFillColor (0, 255,0);
		$this->Cell ( 145, 6,"VAT 7%", 'LTRB', 0, 'L',true);
		$vat=(7*$sum)/100;
		$this->Cell ( 45, 6, number_format ($vat),'LTRB',0, 'R',true);
		$this->Ln ();
			$this->SetFillColor (255,255,255);
		$this->Cell ( 145, 6,"รวมทั้งหมด", 'LTRB', 0, 'L',true);
		$sumnet=$sum+$vat;
		$this->Cell ( 45, 6, number_format ($sumnet),'LTRB',0, 'R',true);
		$this->Ln ();
	}
	

	
	function orderstatus($status){
		switch ($status) {
			case "000":	return "รอการมัดจำ";
			break;
			case "001":return "รอการชำระเงิน"	;
			break;
			case "011":return "รอการจัดส่ง";
			break;
			case "111":return "เสร็จสิ้น";
			break;
			

		}
	}
}

$header = array (
		"ลำดับ",
		"สินค้า",
		"จำนวน",
		"ราคา" 
);

$pdf = new PDF ();

$pdf->AddFont ( 'EDPenSook' ); // ??????
$pdf->SetFont ( 'EDPenSook', '',  30);
$pdf->AddPage ();
//$pdf->Image('img/aru.png', 50, 90, 120,120);

$pdf->Cell(0,5,"",0,1,"C");
$pdf->Ln(20);
if($_GET["mode"]==1){
$pdf->genHeaderCustomer();}
else{
	$pdf->genHeaderEmployee();
	}
$pdf->Ln(10);
if($_GET["mode"]==1){
$pdf->genHeaderTable($header);}
else{
$pdf->genHeaderTable2($header);
	}
//$pdf->genHeaderTable($header);
$pdf->Ln(10);
if($_GET["mode"]==1){
$pdf->genFooter();}
else{
$pdf->genFooter2();
	}
//$pdf->genFooter();
$pdf->Output ();
?>